<?php

namespace Xin\LaravelFortify\Validation;

use Illuminate\Support\Facades\Validator as ValidatorFacade;

class Validator
{
	/**
	 * @var array
	 */
	protected $data = [];

	/**
	 * 验证规则
	 * @return array
	 */
	public function rules()
	{
		return [];
	}

	/**
	 * 错误提示信息
	 * @return array
	 */
	public function messages()
	{
		return [];
	}

	/**
	 * 属性字段名
	 * @return array
	 */
	public function fields()
	{
		return [];
	}

	/**
	 * 验证数据
	 * @param array $data
	 * @param string|null $scene
	 * @return array
	 * @throws \Illuminate\Validation\ValidationException
	 */
	public function validate(array $data, string $scene = null)
	{
		$rules = $this->rules();

		if ($scene) {
			$sceneRulesMethod = $scene . 'Rules';
			if (method_exists($this, $sceneRulesMethod)) {
				$rules = array_merge($rules, $this->{$sceneRulesMethod}($data));
			}
		}

		return ValidatorFacade::validate($data, $rules, $this->messages(), $this->fields());
	}

	/**
	 * 验证数据
	 * @param array $data
	 * @param string|null $scene
	 * @return array
	 * @throws \Illuminate\Validation\ValidationException
	 */
	public static function validated(array $data, string $scene = null)
	{
		return (new static)->validate($data, $scene);
	}
}
